﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Windows.Forms;
namespace ForkCarRelationManager.log
{
    public class LogWrite
    {
        private string time;

        private string getTime()
        {
            DateTime now = new DateTime();
            now = DateTime.Now;
            string strYMD = now.ToString("D");
            this.time = now.ToString("f");
            return strYMD;
        }

        public void WriteFile(string str)
        {
            try
            {

                string path = Application.StartupPath;
                if (!Directory.Exists(path + "\\Log"))//若文件夹不存在则新建文件夹   
                {
                    Directory.CreateDirectory(path + "\\Log"); //新建文件夹   
                }


                StreamWriter sr;
                string fileName = path + "\\Log\\" + getTime() + "_Log.txt";

                if (File.Exists(fileName)) //如果文件存在,则创建File.AppendText对象
                {
                    sr = File.AppendText(fileName);
                }
                else//如果文件不存在,则创建File.CreateText对象
                {
                    sr = File.CreateText(fileName);
                }
                sr.WriteLine(this.time);
                sr.WriteLine(str);
                sr.WriteLine("---------------------------------------------------");
                sr.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("日志写入错误！");
            }
        }
    }
}
